package com.xyzwps.server.paimon.jackson

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import kotlin.reflect.KClass

object JSON {

    private val paimonModule = SimpleModule().apply {
        this.registerInstant()
        this.registerPhone()
        this.registerUserId()
        this.registerVcodeScene()
    }

    val OM = ObjectMapper().apply {
        registerModule(paimonModule)
        registerModule(JavaTimeModule())
        registerModule(KotlinModule.Builder().build())
    }

    fun stringify(obj: Any?): String = OM.writeValueAsString(obj)

    fun <T> parse(json: String, type: Class<T>): T = OM.readValue(json, type)

    fun <T : Any> parse(json: String, type: KClass<T>): T = parse(json, type.java)
}